package com.czl.lunt.service.impl;

import com.czl.lunt.Exception.ApplicationException;
import com.czl.lunt.common.Result;
import com.czl.lunt.common.ResultCode;
import com.czl.lunt.dao.UserMapper;
import com.czl.lunt.model.Board;
import com.czl.lunt.model.User;
import com.czl.lunt.service.UserService;
import com.czl.lunt.utils.MD5Utils;
import com.czl.lunt.utils.StringUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public void createUser(User user) {
        //进行非空校验
        if(user == null || StringUtils.isEmpty(user.getUsername())
        || StringUtils.isEmpty(user.getPassword()) ||
                StringUtils.isEmpty(user.getSalt())){
            log.error("用户传入参数异常");
            throw new ApplicationException("用户传入参数异常");
        }
        //按用户名查询用户信息
        User EUser = userMapper.selectByUserName(user.getUsername());
        if(EUser != null){
            log.info("用户信息不存在");
            throw new ApplicationException(Result.failed());
        }
        user.setGender((byte) 2);
        user.setArticleCount(0);
        user.setIsAdmin((byte) 0);
        user.setState((byte) 0);
        user.setDeleteState((byte) 0);
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);

        int row = userMapper.insertSelective(user);
        if(row != 1){
            log.error("新增失败");
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL));
        }
        log.info("新增用户成功 {}" , user.getNickname());
    }

    @Override
    public User selectByUserName(@NonNull String userName) {
        return userMapper.selectByUserName(userName);
    }

    @Override
    public User login(@NonNull String userName,@NonNull String password) {
        User user = selectByUserName(userName);
        if(user == null){
            log.info("用户不存在");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_LOGIN));
        }
        //对密码进行校验
        String password_R = MD5Utils.md5Salt(password, user.getSalt());
        //密码错误
        if (!password_R.equalsIgnoreCase(user.getPassword())) {
            log.info("密码错误");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_LOGIN));
        }

        return user;
    }

    @Override
    public User selectById(@NonNull Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addOneArticleById( @NonNull Long id) {
        if(id < 0){
            log.info("帖子数量小于0");
            throw new ApplicationException("帖子数量小于0");
        }

        User user = userMapper.selectByPrimaryKey(id);

        if(user == null){
            log.info("板块数据异常");
            throw new ApplicationException("板块数据异常");
        }
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setArticleCount(user.getArticleCount() + 1);
        int end = userMapper.updateByPrimaryKeySelective(updateUser);
        if(end != 1){
            log.info("受影响行数不等于1");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_BANNED));
        }
    }


    @Override
    public void subOneArticleById(Long id) {
        if(id < 0){
            log.info("帖子数量小于0");
            throw new ApplicationException("帖子数量小于0");
        }

        User user = userMapper.selectByPrimaryKey(id);

        if(user == null){
            log.info("板块数据异常");
            throw new ApplicationException("板块数据异常");
        }
        User updateUser = new User();
        updateUser.setId(user.getId());
        int count = user.getArticleCount() - 1;
        if(count < 0){
            log.info("别删了 ，已经没有帖子啦");
            throw new ApplicationException("别删了 ，已经没有帖子啦");
        }
        updateUser.setArticleCount(count);
        int end = userMapper.updateByPrimaryKeySelective(updateUser);
        if(end != 1){
            log.info("受影响行数不等于1");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_BANNED));
        }

    }

}
